package com.olc.railing.services;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.olc.railing.base.BaseApplication;
import com.olc.railing.utils.FileSizeUtils;
import com.olc.railing.utils.FileUtils;
import com.olc.railing.utils.MyLog;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class DelRedFileServices extends Service {
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    handler.removeMessages(0);
                    if(FileSizeUtils.isNoAvailableStorage()){
                        delRedFile();
                    }else{
                        handler.sendEmptyMessageDelayed(0,60*1000);
                    }
                    break;
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyLog.d("fileService","onStartCommand = ");
        handler.sendEmptyMessage(0);
      //  delRedFile();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void delRedFile() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                File file = new File( BaseApplication.SD_PATH +"/railing");
                File[] files = file.listFiles();
                orderByDate(files);//递增
                MyLog.e("fileService", "files length:" + files.length);
                if(files!=null&&files.length>0){
                    MyLog.e("fileService", " files[0] : " +  files[0].getAbsolutePath());
                    if(FileUtils.deleteFile(files[0])){
                        FileUtils.deleteFile(BaseApplication.SD_PATH+"/LOST.DIR");
                    }
                    handler.sendEmptyMessageDelayed(0,10000);
                }
            }
        }.start();
    }

    /**
     * 按文件修改时间排序
     */
    public static ArrayList<String> orderByDate( File[] files) {
        ArrayList<String> FileNameList = new ArrayList<String>();
        Arrays.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }

        });

        for (File file1 : files) {
            if (file1.isDirectory()) {
                FileNameList.add(file1.getName());
            }
        }
        return FileNameList;
    }

}
